package inet.app.view.widget;

import java.util.ArrayList;

import org.json.JSONObject;

import inet.app.R;
import inet.app.UpdateWidgetService;
import inet.app.controller.connect.JsonParser;
import inet.app.controller.parser.Parser_Kqxs;
import inet.app.controller.request.Request;
import inet.app.model.object.KqGiai_Item;
import inet.app.model.util.DateProc;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;

public class MyWidgetProvider extends AppWidgetProvider{
	private static final String LOG = "android.widget.xoso3mien";
	private static String date_curent="",strContent="",json="";
	private static Request request = new Request();
	private static ArrayList<KqGiai_Item> arraylist = new ArrayList<KqGiai_Item>();
	private static Parser_Kqxs parser = new Parser_Kqxs();
	private static String []stem,stem5;
	private static String giai3,giai5;
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		// TODO Auto-generated method stub
		super.onUpdate(context, appWidgetManager, appWidgetIds);
		date_curent = DateProc.Timestamp2DDMMYYYY(DateProc.createTimestamp());
		
		Log.v(LOG, "onUpdate method called");
		// Get all ids
		ComponentName thisWidget = new ComponentName(context, MyWidgetProvider.class);
		int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
		// REQUEST DATA
					AsyncTask<Void, Integer, Void> myTask = new AsyncTask<Void, Integer, Void>() {
						protected Void doInBackground(Void... params) {
							arraylist = parser.getList(request.getkqxsbyregion(date_curent, "MB"));
							return null;
						}
						protected void onPostExecute(Void result) {
							super.onPreExecute();
							if (arraylist.size()>=1) {
								
								if (arraylist.get(0).getGiai3().length()>15) {
									stem = arraylist.get(0).getGiai3().replace(" ", "").split("-");
									giai3= "  "+stem[0]+"-"+ stem[1]+"-"+ stem[2]+"\n\t\t"+ stem[3]+ "-"+ stem[4]+"-"+ stem[5];
								}else {
									giai3= arraylist.get(0).getGiai3();
								}
								
								if (arraylist.get(0).getGiai5().length()>15) {
									stem = arraylist.get(0).getGiai5().replace(" ", "").split("-");
									giai5= "\t  "+stem[0]+ "-"+stem[1]+ "-"+stem[2]+"\n\t\t\t"+ stem[3]+"-"+ stem[4]+"-"+ stem[5];
								}else {
									giai5= arraylist.get(0).getGiai5();
								}
								
								strContent=" DB: " +"\t\t\t"+ arraylist.get(0).getGiaidb()+"\n"
										+" G1: " + "\t\t\t"+arraylist.get(0).getGiai1() + "\n"
										+" G2: " + "\t\t"+arraylist.get(0).getGiai2() + "\n"
										+" G3: " + giai3 + "\n"
										+" G4: " + arraylist.get(0).getGiai4() + "\n"
										+" G5: " + giai5 + "\n"
										+" G6: " +"\t\t  "+ arraylist.get(0).getGiai6() + "\n"
										+" G7: " +"\t\t  "+ arraylist.get(0).getGiai7() + "\n";
							}
						}
					};
					myTask.execute();
				//END
		// Build the intent to call the service
		Intent intent = new Intent(context.getApplicationContext(), UpdateWidgetService.class);
		intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
		intent.putExtra("json", strContent);

		// Update the widgets via the service
		context.startService(intent);
	
	}
}
